<template>
  <div class="welcome-container">
    <el-row :gutter="20">
      <el-col :span="24">
        <el-card class="welcome-card">
          <template #header>
            <div class="card-header">
              <h2>欢迎使用金融信贷管理系统</h2>
            </div>
          </template>
          <div class="welcome-content">
            <el-row :gutter="24">
              <el-col 
                :span="12" 
                v-for="(item, index) in statistics" 
                :key="index"
                style="margin-bottom: 24px"
              >
                <div class="stat-card">
                  <el-icon :size="48" :class="item.color" style="margin-bottom: 16px">
                    <component :is="item.icon" />
                  </el-icon>
                  <div class="stat-info">
                    <div class="stat-value">{{ item.value }}</div>
                    <div class="stat-label">{{ item.label }}</div>
                  </div>
                </div>
              </el-col>
            </el-row>
          </div>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { getSystemOverview } from '@/api/system'
import { formatCurrency } from '../utils/format'

const statistics = ref([
  {
    label: '贷款总额',
    value: '¥0',
    icon: 'Money',
    color: 'text-success'
  },
  {
    label: '客户总数',
    value: '0',
    icon: 'User',
    color: 'text-primary'
  },
  {
    label: '逾期笔数',
    value: '0',
    icon: 'Document',
    color: 'text-warning'
  },
  {
    label: '本月放款',
    value: '¥0',
    icon: 'Wallet',
    color: 'text-danger'
  }
])

const fetchOverviewData = async () => {
  try {
    const { data } = await getSystemOverview()
    if (data) {
      statistics.value = [
        {
          label: '贷款总额',
          value: formatCurrency(data.totalLoanAmount),
          icon: 'Money',
          color: 'text-success'
        },
        {
          label: '客户总数',
          value: data.totalClients.toString(),
          icon: 'User',
          color: 'text-primary'
        },
        {
          label: '逾期笔数',
          value: data.overdueCount.toString(),
          icon: 'Document',
          color: 'text-warning'
        },
        {
          label: '本月放款',
          value: formatCurrency(data.currentMonthDisbursement),
          icon: 'Wallet',
          color: 'text-danger'
        }
      ]
    }
  } catch (error) {
    console.error('获取系统概览数据失败:', error)
  }
}

onMounted(() => {
  fetchOverviewData()
})
</script>

<style scoped>
.welcome-container {
  padding: 20px;
}

.welcome-card {
  margin-bottom: 20px;
}

.card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.card-header h2 {
  margin: 0;
  font-size: 18px;
  font-weight: 600;
}

.welcome-content {
  padding: 20px 0;
}

.stat-card {
  background-color: #fff;
  border-radius: 8px;
  padding: 24px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.08);
  min-height: 160px;
  transition: all 0.3s ease;
}

.stat-card:hover {
  transform: translateY(-4px);
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.12);
}

.stat-info {
  flex: 1;
}

.stat-info {
  display: flex;
  flex-direction: column;
  justify-content: center;
  width: 100%;
}

.stat-value {
  font-size: 28px;
  font-weight: 600;
  margin-bottom: 8px;
  text-align: center;
}

.stat-label {
  color: #999;
  font-size: 20px;
  letter-spacing: 0.5px;
  text-align: center;
}

.text-primary {
  color: #409EFF;
}

.text-success {
  color: #67C23A;
}

.text-warning {
  color: #E6A23C;
}

.text-danger {
  color: #F56C6C;
}
</style>